package com.qf.co.controller;


import com.qf.co.pojo.Profile;
import com.qf.co.service.ProfileService;
import com.qf.co.service.ProfileServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
//你是誰
/**
 * @author hanjiejie
 * @version V1.0
 * @Project java2203
 * @Package com.qf.co.controller
 * @Description:
 * @Date 2022/5/28 21:59
 */
@WebServlet("/goods")
public class profileController extends BaseServlet{
    public String show(HttpServletRequest request, HttpServletResponse response){
        ProfileService profileService = new ProfileServiceImpl();
        List<Profile> list = profileService.findProfile();
        if (list ==null){
           return null;
        }
        //将商品数据放入request 域对象中
        request.getSession().setAttribute("List", list);
//        request.setAttribute("List", list );
        return "redirect:/List.jsp";
    }

    /**
     * 通过id查找用户详情
                * @param request
                * @param response
                * @return
     */
        public String AllOne(HttpServletRequest request,HttpServletResponse response){
        String id = request.getParameter("id");
        ProfileService profileService = new ProfileServiceImpl();
        Profile profile = profileService.allone(id);
        if (profile == null){
           return null;
        }
        //将商品信息放入request中
        request.getSession().setAttribute("profileList", profile);
//       request.setAttribute("profileList", profile);
        return  "redirect:/detail.jsp";
    }

    /**\
     *
     * @param request
     * @param response
     * @return
     */
    public String update(HttpServletRequest request,HttpServletResponse response){
        String id = request.getParameter("id");
        ProfileService profileService = new ProfileServiceImpl();
        Profile profile = profileService.allone(id);
        if (profile == null){
            return null;
        }
        request.setAttribute("profileList", profile);
        return  "forward:/update.jsp";
    }
    public String updatea(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException, InvocationTargetException, IllegalAccessException {
//        String id = request.getParameter("id");
//        String name = request.getParameter("name");
//        String birthday = request.getParameter("birthday");
//        String gender = request.getParameter("gender");
//        String career = request.getParameter("career");
//        String address = request.getParameter("address");
//        String mobile = request.getParameter("mobile");
//        String picture = request.getParameter("picture");
        Map<String, String[]> parameterMap = request.getParameterMap();
        Profile profile = new Profile();

        BeanUtils.populate(profile,parameterMap);

//        Profile profile = new Profile(Integer.parseInt(id),name,birthday,gender,career,address,mobile,picture);
        ProfileService profileService = new ProfileServiceImpl();
        Boolean count = profileService.update(profile);
        if (count){
            //更改后創新跳轉
            return "redirect:/goods?method=show";
        }
        return null;
    }
}
